
		****************************************************************************************************
		* 		Productivity and Health:  Alternative Productivity Measures using Physical Activity 	   *
		* 																  								   *
		*		Authors: Andrew Dillon, Jed Friedman, Pieter Serneels, Oladele Akogun, Ashesh Prasann	   *
		*       Code contributed by: Tyler Box                                  	 			           *      
		* 		Published in: World Bank Economic Review												   *
		*		Last Edited: TB, March 30th, 2020 				      			      					   *
		* 		STATA Version 15												  						   *
		****************************************************************************************************


clear
clear matrix
set more off

cap ssc install egenmore
cap ssc install xml_tab

cap net install sg97_5.pkg
cap net install mat2txt.pkg
cap net install rmfiles.pkg


global datadir "C:/Users/tjb0217/Box Sync/Tyler/Nigeria Malaria/WBER/Public Disclosure - 03202020/"
cd "${datadir}"

*************
** Online Appendix Table 2 **
*************
{
use "${datadir}wages_health_worker_fitbit_generated_public.dta", clear

*Drops all dates that are repeated (last day has 2 of each fitbit obs). Also fitbit 65 has repeats
sort Fitbit Date
drop if Date==Date[_n-1] & wear_fitbit==1

*MISSING DATES BETWEEN MAR 20 AND APRIL 2. Calculating loss conditional on being lost before Mar 20
gen lost_cutoff=0
replace lost_cutoff=1 if LOST==1 & Date<19437

drop if lost_cutoff==1

drop if MinutesLightlyActive==0 &  MinutesFairlyActive==0 &  MinutesVeryActive==0

gen min_sed_light=MinutesSedentary + MinutesLightlyActive
gen min_fair_very=MinutesFairlyActive + MinutesVeryActive
gen min_active=MinutesSedentary + MinutesLightlyActive + MinutesVeryActive


g primary=0
replace primary=1 if school>10 & school<=16

g junior_sec=0
replace junior_sec=1 if school>20 & school<=24 
g senior_sec=0
replace senior_sec=1 if school>23 & school<=27


bys Fitbit, sort: gen nvals = _n == 1
count if nvals
drop nvals

global balance "age bmi rdtp haemo hh_size spouses spouses_living_in_hh num_children_in_hh marital cattle poultry hh_rooms hhasset_m1D read write educ edyrs math school primary junior_sec senior_sec experience exfyr"

collapse $balance gang week_int_r4 wear_fitbit Fitbit, by (plantation_id_n)


bys Fitbit, sort: gen nvals = _n == 1
count if nvals

****************************************************************
***Fitbit Sample distribution across gangs and weeks***
****************************************************************
tab gang week_int_r4 if wear_fitbit==1

*************************************************************
***Online Appendix Table 2: Balance across Tracked & Untracked Sub-samples***
*************************************************************

preserve
use "${datadir}Fitbit_Worker_AllDays_Trimmed_v3.dta", clear
collapse (mean) daily_amount_allday work, by(plantation_id_n)

tempfile balanceinfo
save `balanceinfo'
restore

merge 1:1 plantation_id_n using `balanceinfo', keepusing(daily_amount_allday work)

ds educ rdtp
foreach var in `r(varlist)' {
	gen `var'_t100 = (`var' * 100)
}

ds experience educ_t100 primary junior_sec senior_sec bmi rdtp_t100 haemo hh_size hhasset_m1D
foreach var in `r(varlist)' {
	bys wear_fitbit: sum `var'
}

foreach var in experience educ_t100 primary junior_sec senior_sec bmi rdtp_t100 haemo hh_size hhasset_m1D {
	forvalues n=0/1 {
			sum `var' if wear_fitbit==`n' 
			matrix N`var'`n'=r(N)
			matrix m`var'`n'=r(mean)
			matrix sd`var'`n'=r(sd)
		}
		ttest `var', by (wear_fitbit) 
		matrix `var'= N`var'0, m`var'0, sd`var'0, N`var'1, m`var'1, sd`var'1
		matrix rownames `var'=`var'
		
reg `var' wear_fitbit
testparm wear_fitbit
local pvalue_all=r(p)
eststo `var'_a, addscalars(pvalue_all `pvalue_all')
}
xml_tab *_a, save(${datadir}tables\Appendix\T2_regs.xls) stats(N pvalue_all) title (Balancing across Tracked and Untracked) sd2 replace sheet(Worker Characteristics) format(SCLR3 NCLR3)

matrix a= experience\educ_t100\primary\junior_sec\senior_sec\bmi\rdtp_t100\haemo\hh_size\hhasset_m1D

frmttable using "${datadir}tables\Appendix\T2_means.doc", replace sdec(0, 2, 2,0, 2, 2) statmat(a) varlabels ///
	title("Table 2: Balancing") coljust(l;c)  ///
	ctitles("" ,N(NON-FITBITS) , Mean, SD, N(FITBITS), Mean, SD) 

}

*******************************************
***Appendix Table 3: Determinants of Tracker Loss***
*******************************************
{
eststo clear

use "${datadir}wages_health_worker_fitbit_generated_public.dta", clear
*Drops all dates that are repeated (last day has 2 of each fitbit obs). Also fitbit 65 has repeats
bys Fitbit Date: drop if Date==Date[_n-1] 

sort Fitbit Date
drop lost_duration kept_duration

*MISSING DATES BETWEEN MAR 20 AND APRIL 2. Calculating loss conditional on being lost before Mar 20
gen lost_cutoff=0
replace lost_cutoff=1 if LOST==1 & Date<19437


egen lost_duration = count(LOST), by(Fitbit)
egen kept_duration = count(Fitbit) if LOST==., by(Fitbit)

drop if MinutesLightlyActive==0 &  MinutesFairlyActive==0 &  MinutesVeryActive==0

replace week_int_r4=1 if week_int_r4>1 & week_int_r4<2
replace gang=3 if gang>3 & gang<4


sort plantation_id_n wr4_A_6_DATE
gen pos=1 if rdtp==1
gen neg=1 if rdtp==0


capture drop tag
egen tag=tag(plantation_id_n wr4_A_6_DATE)

bys Fitbit, sort: gen nvals = _n == 1
count if nvals

collapse lost_cutoff lost_duration kept_duration educ rdtp pos neg LOST hh_rooms wear_fitbit age read write school edyrs experience exfyr num_children_in_hh marital rooms_main mono poly cattle poultry haemo treat bmi week_int_r4 gang plantation_id_n, by(Fitbit)

bys Fitbit, sort: gen nvals = _n == 1
count if nvals


g primary=0
replace primary=1 if school>10 & school<16

g junior_sec=0
replace junior_sec=1 if school>20 & school<24 
g senior_sec=0
replace senior_sec=1 if school>23 & school<27
tab educ
replace educ = 1 if educ > 0 & educ < 1
tab rdtp
replace rdtp = 1 if rdtp > 0 & rdtp < 1
tab  lost_cutoff
replace lost_cutoff=1 if lost_cutoff>0
tab lost_cutoff

// prepping the worker IDs that are used in later regressions
preserve

use "${datadir}final_dataset_condensed_public.dta", clear

collapse hr_total , by(plantation_id_n)


tempfile workers
save `workers'

restore

merge 1:1 plantation_id_n using `workers'

reg lost_cutoff age educ exfyr num_children_in_hh mono poly cattle poultry rooms_main haemo rdtp bmi if _merge == 3, vce (cluster gang)
outreg2 using "${datadir}tables\Appendix\T3_all.xls", replace bdec(2) 

drop _merge
}

*******************************************************
***Fig 1: Days Before Tracker Loss (Before Stoppage)***
*******************************************************

gen duration_bin = kept_duration
replace duration_bin = 10 if duration_bin <=15
replace duration_bin = 20 if duration_bin <=25 & kept_duration>15
replace duration_bin = 30 if duration_bin <=35 & kept_duration>25
replace duration_bin = 40 if duration_bin <=45 & kept_duration>35
replace duration_bin = 50 if duration_bin <56 & kept_duration>45


histogram duration_bin if duration_bin<56, frequency xtitle("Number of Days Before Tracker Loss") bcolor(gray) discrete width(10)
graph save Graph "${datadir}graphs\Fig1_trackerloss.gph", replace

tab kept_duration
tab duration_bin

*******************************************************************************************************************************************************************************************************

*********************************************************
****** Figures 2: Minutes Sedentary and Compliance ******
*********************************************************
{

use "${datadir}wages_health_worker_fitbit_generated_public.dta", clear

*Replace typos with negative minutes
replace MinutesSedentary=292 if MinutesSedentary==-292
replace MinutesSedentary=14 if MinutesSedentary==-14

*Drops all dates that are repeated (last day has 2 of each fitbit obs). Also fitbit 65 has repeats
bys Fitbit Date: drop if Date==Date[_n-1] 
sort Fitbit Date

*MISSING DATES BETWEEN MAR 20 AND APRIL 2. Calculating loss conditional on being lost before Mar 20
gen lost_cutoff=0
replace lost_cutoff=1 if LOST==1 & Date<19437

*Restrict sample to after Feb 16, when study begins. Also restrict sample to only workers allocated fitbits
keep if wr4_A_6_DATE>19405


keep if wear_fitbit==1
drop if lost_cutoff==1
drop if ActiveScore==.
drop if MinutesLightlyActive==0 &  MinutesFairlyActive==0 &  MinutesVeryActive==0



gen productivity_date=Date
gen health_int_date=wr4_A_6_DATE

gen post_int=0
replace post_int=1 if productivity_date>health_int_date

g post_treat_date=health_int_date+5
format post_treat_date %td
gen post_treat=post_int
replace post_treat=0 if productivity_date>post_treat_date

g post_treat_date2=health_int_date+3
format post_treat_date2 %td
gen post_treat2=post_int
replace post_treat2=0 if productivity_date>post_treat_date2

g t_minus7=0
replace t_minus7=1 if productivity_date>health_int_date-8 & productivity_date<health_int_date
g t_plus7=0
replace t_plus7=1 if productivity_date<health_int_date+8 & productivity_date>health_int_date

g t_minus15=0
replace t_minus15=1 if productivity_date>health_int_date-16 & productivity_date<health_int_date
g t_plus15=0
replace t_plus15=1 if productivity_date<health_int_date+16 & productivity_date>health_int_date

g amount_day_un=daily_amount
replace amount_day_un=0 if daily_amount==.
g rods_day_un=daily_rods
replace rods_day_un=0 if daily_rods==.

g ln_amount=ln(amount_day_un)
g ln_rods=ln(rods_day_un)

egen gang_day = group(gang productivity_date)
tab gang_day, gen(gangBYday)

gen min_active=MinutesLightlyActive+MinutesFairlyActive+MinutesVeryActive
gen hr_active=(MinutesLightlyActive+MinutesFairlyActive+MinutesVeryActive)/60
gen hr_sed_light=(MinutesSedentary + MinutesLightlyActive)/60
gen hr_fair_very=(MinutesFairlyActive + MinutesVeryActive)/60
gen hr_sedentary=MinutesSedentary/60
gen hr_total=hr_active+hr_sedentary
gen hr_sed=MinutesSedentary/60
gen hr_light=MinutesLightlyActive/60
gen hr_fair=MinutesFairlyActive/60
gen hr_very=MinutesVeryActive/60

tab productivity_date if hr_total<24


**TRIMMING CRITERIA**

*1. Drop if Fitbit does not add to 24 hours. No real concentration by date. Spread over entire period
gen hours_not24=0
replace hours_not24=1 if hr_total<24

*3. Trimmed data based on minutes sedentary distribution to identify non-compliance*

g exclude=0
replace exclude=1 if hours_not24==1


*2. Winsorize rods per day as no clear biological threshold for cutting cane
g rods_per=daily_rods/hr_active if exclude==0
sum rods_per, detail
gen touse = inrange(rods_per, r(p1), r(p99)) if exclude==0
replace exclude=1 if touse==0

*30 obs deleted
**Alternative approach**
sum rods_per, detail
gen touse2 = inrange(rods_per, r(p0), r(p95))
tab exclude

drop if exclude == 1


// Sedentary
histogram MinutesSedentary if work == 0, width(15) yscale(range(0 0.01)) ylabel(0(0.002)0.01) bcolor(gray) title("Minutes Sedentary, Work == 0")
graph save Graph "${datadir}\graphs\Fig2_sedentary_nowork.gph", replace

histogram MinutesSedentary if work == 1, width(15) yscale(range(0 0.01)) ylabel(0(0.002)0.01) bcolor(gray) title("Minutes Sedentary, Work == 1")
graph save Graph "${datadir}\graphs\Fig2_sedentary_work.gph", replace

// Lightly Active
histogram MinutesLightlyActive if work == 0, width(15) yscale(range(0 0.015)) ylabel(0(0.0025)0.015) bcolor(gray) title("Minutes Lightly Active, Work == 0")
graph save Graph "${datadir}\graphs\Fig2_lightact_nowork.gph", replace

histogram MinutesLightlyActive if work == 1, width(15) yscale(range(0 0.015)) ylabel(0(0.0025)0.015) bcolor(gray) title("Minutes Lightly Active, Work == 1")
graph save Graph "${datadir}\graphs\Fig2_lightact_work.gph", replace

// Fairly Active
histogram MinutesFairlyActive if work == 0, width(15) yscale(range(0 0.025)) ylabel(0(0.005)0.025) bcolor(gray) title("Minutes Fairly Active, Work == 0")
graph save Graph "${datadir}\graphs\Fig2_fairact_nowork.gph", replace

histogram MinutesFairlyActive if work == 1, width(15) yscale(range(0 0.025)) ylabel(0(0.005)0.025) bcolor(gray) title("Minutes Fairly Active, Work == 1")
graph save Graph "${datadir}\graphs\Fig2_fairact_work.gph", replace

// Very Active
histogram MinutesVeryActive if work == 0, width(15) yscale(range(0 0.05)) ylabel(0(0.01)0.05) bcolor(gray) title("Minutes Very Active, Work == 0")
graph save Graph "${datadir}\graphs\Fig2_veryact_nowork.gph", replace

histogram MinutesVeryActive if work == 1, width(15) yscale(range(0 0.05)) ylabel(0(0.01)0.05) bcolor(gray) title("Minutes Very Active, Work == 1")
graph save Graph "${datadir}\graphs\Fig2_veryact_work.gph", replace




histogram MinutesSedentary if work == 1, width(15) bcolor(gray) title("Minutes Sedentary, Work == 1")
graph save Graph "${datadir}graphs\Fig2_workday.gph", replace

histogram MinutesSedentary if work == 0, width(15) bcolor(gray) title("Minutes Sedentary, Work == 0")
graph save Graph "${datadir}graphs\Fig2_nonworkday.gph", replace

histogram MinutesSedentary if rdtp == 1, width(15) bcolor(gray) title("Minutes Sedentary, Positive for Malaria")
graph save Graph "${datadir}graphs\Fig2_malariapos.gph", replace

histogram MinutesSedentary if rdtp == 1, width(15) bcolor(gray) title("Minutes Sedentary, Positive for Malaria & Work == 1")
graph save Graph "${datadir}graphs\Fig2_malariapos_workday.gph", replace

histogram MinutesSedentary if rdtp == 1, width(15) bcolor(gray) title("Minutes Sedentary, Positive for Malaria & Work == 0")
graph save Graph "${datadir}graphs\Fig2_malariapos_nonworkday.gph", replace

histogram MinutesSedentary if rdtp == 0, width(15) bcolor(gray) title("Minutes Sedentary, Negative for Malaria")
graph save Graph "${datadir}graphs\Fig2_malarianeg.gph", replace

histogram MinutesSedentary if rdtp == 0, width(15) bcolor(gray) title("Minutes Sedentary, Negative for Malaria & Work == 1")
graph save Graph "${datadir}graphs\Fig2_malarianeg_workday.gph", replace

histogram MinutesSedentary if rdtp == 0, width(15) bcolor(gray) title("Minutes Sedentary, Negative for Malaria & Work == 0")
graph save Graph "${datadir}graphs\Fig2_malarianeg_nonworkday.gph", replace



}

******************************
****  Main Paper Table 2-5 Prep    ****
******************************

use "${datadir}Fitbit_Worker_AllDays_Trimmed_public.dta", clear

// Setting up different trimming options
ren outlier outlier_v1

gen trimmed_obs_a = 0
replace trimmed_obs_a = 1 if (hr_total != 24 & !missing(hr_total))

replace trimmed_obs_a = 1 if (hr_sedentary >= 21 & work_allday == 1 & !missing(hr_total))
replace trimmed_obs_a = 1 if (hr_sedentary >= 22.5 & work_allday == 0 & !missing(hr_total))

gen trimmed_obs_b = 0
replace trimmed_obs_b = 1 if (hr_total != 24 & hr_total != .)

replace trimmed_obs_b = 1 if (hr_sedentary >= 19 & work_allday == 1 & hr_total != .)
replace trimmed_obs_b = 1 if (hr_sedentary >= 20.5 & work_allday == 0 & hr_total != .)


gen trimmed_obs_c = 0
replace trimmed_obs_c = 1 if (hr_total != 24 & hr_total != .)

replace trimmed_obs_c = 1 if (hr_sedentary >= 17 & work_allday == 1 & hr_total != .)
replace trimmed_obs_c = 1 if (hr_sedentary >= 18.5 & work_allday == 0 & hr_total != .)


sum rods_per_AD if work_allday == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work_allday == 1
replace trimmed_obs_a = 1 if outlier == 1
replace trimmed_obs_b = 1 if outlier == 1
replace trimmed_obs_c = 1 if outlier == 1

g r_sed_act=hr_sedentary/hr_active
g r_lig_act=hr_light/hr_active
g r_fvr_act=hr_fair_very/hr_active
g r_fai_act=hr_fair/hr_active
g r_ver_act=hr_very/hr_active

g r_lig_tot=hr_light/hr_total
g r_fvr_tot=hr_fair_very/hr_total
g r_fai_tot=hr_fair/hr_total
g r_ver_tot=hr_very/hr_total

save "${datadir}Fitbit_Worker_AllDays_Trimmed_v2.dta", replace


// Prepping data //
{
**10-day comparison period***
* stacking the daily data first for the 10 day comparison period
*to do this first need to identify the date-pairs of interview that are approximately 10 days apart and loop through each pair
*need to refer to spreadsheet that determines these day-pairs, the treated of each day-pair listed below
foreach i in 19407 19409 19410 19411 19414 19415 19416 19417 19418 19419 19421 19422 19423 19425 {
	use "${datadir}Fitbit_Worker_AllDays_Trimmed_v2.dta", clear
	*first generating a dummy var equal to the "treated" date of interview
	gen groupvar = `i'
	gen T_7 = 1 if (health_int_date == `i')
	replace T_7 = 0 if (health_int_date == `i'+7)
		*matching some days to 9 or 11 day gaps when there is no 10 day exact gap
		replace T_7 = 0 if (groupvar == 19417 & health_int_date == 19423)
		replace T_7 = 0 if (groupvar == 19417 & health_int_date == 19425)
		replace T_7 = 0 if (groupvar == 19422 & health_int_date == 19428)
		replace T_7 = 0 if (groupvar == 19423 & health_int_date == 19431)	
	*taking all days of observation from 7 days before treament to 14 days after
	gen studydays = 1 if ((Date > `i' & Date < `i'+7)  & (T_7 == 1 | T_7 == 0))
	keep if studydays == 1
		if `i' == 19407 {
		save "${datadir}temp.dta", replace
		}
	else {
		append using "${datadir}temp.dta"
		save "${datadir}temp.dta", replace
	}
}



*need to generate a count of days normalized around interview date
sort groupvar plantation_id_n T_7 Date
by groupvar plantation_id_n T_7: gen obsday = Date - groupvar
tab obsday


*now need to generate the gangXday FE
sort gang Date
egen gang_day = group(gang Date)
tab gang_day, gen(gangBYday)

*now creating gang FE
tab gang, gen(gangFE)


save "${datadir}Fitbit_Worker_AllDays_Trimmed_v3.dta", replace



sort plantation_id_n T_7
egen balance_id = group(plantation_id_n T_7) // check this

tab T_7, m

sort balance_id
collapse (mean) plantation_id_n work rdtp edyrs bmi haemo gang T_7 (count) studydays, by(balance_id)

*now merging with other covars
sort plantation_id_n
merge m:1 plantation_id_n using "${datadir}Fitbit_Worker_Covars.dta"

tab _m 
drop _m



tab T_7, m
keep if T_7 != .

gen sch=1 if edyrs==0 
gen prim=1 if edyrs==1 
gen jrse=1 if edyrs==5 
gen srse=1 if edyrs==12 | edyrs==13 

recode sch prim jrse srse (.=0) 
}
*************
** Main Paper Table 4 **
*************

local j = 0

gen hhasset_m1D_2 = (hhasset_m1D / 1000)



foreach covar in exfyr sch prim jrse srse hh_size hhasset_m1D_2 bmi haemo work rdtp hh_rooms gang {
	di "`covar'"
	local ++j
	ttest `covar', by(T_7)
	matrix ttest= ((r(mu_1) - r(mu_2)), r(p), r(mu_1), r(N_1), r(mu_2), r(N_2))
	matrix rownames ttest= `covar'
	matrix colnames ttest= mean_differences pvalue mean1 N1 mean2 N2 
		mat2txt, matrix(ttest) saving("${datadir}tables\Appendix\T4_all.xls") `=cond(`j' == 1, "replace","append")'
	gen `covar'_nmd = abs(r(mu_1)-r(mu_2))/r(sd)
	* this is the normalized mean difference variable
}




foreach covar in exfyr sch prim jrse srse hh_size hhasset_m1D bmi haemo work rdtp hh_rooms gang {
	di "`covar'"
	reg `covar' T_7, vce(cluster gang)
	testparm T_7
	local pvalue_all=r(p)
	eststo `covar'_reg, addscalars(pvalue_all `pvalue_all')
}	




use "${datadir}final_dataset_condensed_public.dta", clear

gen rods_per = daily_rods/hr_active

sum work
bysort post_int: sum work
bysort rdtp: sum work

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

sum work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very ActiveScore

sum rods_per if work == 1
bysort post_int: sum rods_per if work == 1
bysort rdtp: sum rods_per if work == 1



***********************
** Appendix Table 1 part 1 **
***********************

order work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very

outreg2 using "${datadir}tables/Appendix/T5_allobs.xls", sum(log) replace keep(work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very ActiveScore) eqkeep(N mean sd)


/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 21 & work == 1
drop if hr_sedentary >= 22.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1

sum work
bysort post_int: sum work
bysort rdtp: sum work

sum rods_per if work == 1
bysort post_int: sum rods_per if work == 1
bysort rdtp: sum rods_per if work == 1

sum work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very ActiveScore
sum work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very ActiveScore if work == 1
sum work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very ActiveScore if work == 0


**********************************
** Appendix Table 1 parts 2, 3, and 4 **
**********************************

outreg2 using "${datadir}tables/Appendix/T5_trimmedobs.xls", sum(log) replace keep(work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very ActiveScore) eqkeep(N mean sd)

outreg2 if work == 1 using "${datadir}tables/Appendix/T5_trimmedobs_workdays.xls", sum(log) replace keep(work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very ActiveScore) eqkeep(N mean sd)

outreg2 if work == 0 using "${datadir}tables/Appendix/T5_trimmedobs_nonworkdays.xls", sum(log) replace keep(work daily_amount daily_rods hr_sedentary hr_light hr_fair hr_very ActiveScore) eqkeep(N mean sd)

rmfiles , folder("${datadir}tables\") match(*.txt)




*******************
***** Main Paper Table 2 *****
*******************

***********************************************
********** Worker FEs Worker Cluster **********
***********************************************

// Trim A //
{
preserve

use "${datadir}Fitbit_Worker_AllDays_Trimmed_v2.dta", clear

collapse (max) intweek , by(plantation_id_n productivity_date)

tempfile week
save `week'

restore



use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m


gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */


drop if hr_total != 24 & hr_total != .

drop if hr_sedentary >= 21 & work == 1 & hr_total != .
drop if hr_sedentary >= 22.5 & work == 0 & hr_total != .

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trima_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trima_workerfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\rods_regs_trima_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trima_workerfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\rods_cond_regs_trima_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}

// Trim B //
{
use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m

gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 19 & work == 1
drop if hr_sedentary >= 20.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trimb_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trimb_workerfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\rods_regs_trimb_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trimb_workerfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\rods_cond_regs_trimb_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}

// Trim C //
{
use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m

gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 17 & work == 1
drop if hr_sedentary >= 18.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trimc_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trimc_workerfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very i.plantation_id_n, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\rods_regs_trimc_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\T2_all_trimc_workerfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very i.plantation_id_n if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Worker Cluster\Worker FE\rods_cond_regs_trimc_workerfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}


*****************************************************
********** Gang by Week FEs Worker Cluster **********
*****************************************************

// Trim A //
{
preserve

use "${datadir}Fitbit_Worker_AllDays_Trimmed_v2.dta", clear

collapse (max) intweek , by(plantation_id_n productivity_date)

tempfile week
save `week'

restore



use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m


gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */

drop if hr_total != 24 & hr_total != .

drop if hr_sedentary >= 21 & work == 1 & hr_total != .
drop if hr_sedentary >= 22.5 & work == 0 & hr_total != .

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Main Paper\T2_all_trima_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Main Paper\T2_all_trima_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Main Paper\rods_regs_trima_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Main Paper\T2_all_trima_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Main Paper\rods_cond_regs_trima_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}

// Trim B //
{
use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m

gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 19 & work == 1
drop if hr_sedentary >= 20.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Main Paper\T2_all_trimb_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Main Paper\T2_all_trimb_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Main Paper\rods_regs_trimb_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Main Paper\T2_all_trimb_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Main Paper\rods_cond_regs_trimb_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}

// Trim C //
{
use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m

gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 17 & work == 1
drop if hr_sedentary >= 18.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Main Paper\T2_all_trimc_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Main Paper\T2_all_trimc_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active gang##intweek, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very gang##intweek, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very gang##intweek, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Main Paper\rods_regs_trimc_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Main Paper\T2_all_trimc_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Main Paper\rods_cond_regs_trimc_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}



***************************************************
********** Gang by Week FEs Gang Cluster **********
***************************************************

// Trim A //
{
preserve

use "${datadir}Fitbit_Worker_AllDays_Trimmed_v2.dta", clear

collapse (max) intweek , by(plantation_id_n productivity_date)

tempfile week
save `week'

restore



use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m


gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */

drop if hr_total != 24 & hr_total != .

drop if hr_sedentary >= 21 & work == 1 & hr_total != .
drop if hr_sedentary >= 22.5 & work == 0 & hr_total != .

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active gang##intweek, vce(cluster gang)
estimates store work1
reg work hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store work2
reg work hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trima_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active gang##intweek, vce(cluster gang)
estimates store amount1
reg daily_amount hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trima_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active gang##intweek, vce(cluster gang)
estimates store rods1
reg daily_rods hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\rods_regs_trima_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trima_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\rods_cond_regs_trima_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}

// Trim B //
{
use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m

gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 19 & work == 1
drop if hr_sedentary >= 20.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active gang##intweek, vce(cluster gang)
estimates store work1
reg work hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store work2
reg work hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trimb_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active gang##intweek, vce(cluster gang)
estimates store amount1
reg daily_amount hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trimb_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active gang##intweek, vce(cluster gang)
estimates store rods1
reg daily_rods hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\rods_regs_trimb_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trimb_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\rods_cond_regs_trimb_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}

// Trim C //
{
use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m

gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 17 & work == 1
drop if hr_sedentary >= 18.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active gang##intweek, vce(cluster gang)
estimates store work1
reg work hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store work2
reg work hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trimc_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active gang##intweek, vce(cluster gang)
estimates store amount1
reg daily_amount hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trimc_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active gang##intweek, vce(cluster gang)
estimates store rods1
reg daily_rods hr_light hr_fair_very gang##intweek, vce(cluster gang)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very gang##intweek, vce(cluster gang)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\rods_regs_trimc_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster gang)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\T2_all_trimc_gangweekfe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very gang##intweek if work == 1, vce(cluster gang)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Gang Cluster\GangXweek FE\rods_cond_regs_trimc_gangweekfe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}


*******************************************
********** No FEs Worker Cluster **********
*******************************************

// Trim A //
{
preserve

use "${datadir}Fitbit_Worker_AllDays_Trimmed_v2.dta", clear

collapse (max) intweek , by(plantation_id_n productivity_date)

tempfile week
save `week'

restore



use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m


gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */

drop if hr_total != 24 & hr_total != .

drop if hr_sedentary >= 21 & work == 1 & hr_total != .
drop if hr_sedentary >= 22.5 & work == 0 & hr_total != .

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trima_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trima_nofe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Worker Cluster\No FE\rods_regs_trima_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trima_nofe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Worker Cluster\No FE\rods_cond_regs_trima_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}

// Trim B //
{
use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m

gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 19 & work == 1
drop if hr_sedentary >= 20.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trimb_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trimb_nofe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Worker Cluster\No FE\rods_regs_trimb_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trimb_nofe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Worker Cluster\No FE\rods_cond_regs_trimb_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}

// Trim C //
{
use "${datadir}final_dataset_condensed_public.dta", clear

merge 1:m plantation_id_n productivity_date using `week', keepusing(intweek)
drop if _m != 3
drop _m

gen rods_per = daily_rods/hr_active

gen Min_Active = MinutesLightlyActive + MinutesFairlyActive + MinutesVeryActive
gen Min_FairVery = MinutesFairlyActive + MinutesVeryActive

/* cleaning parameters here */
drop if hr_total != 24

drop if hr_sedentary >= 17 & work == 1
drop if hr_sedentary >= 18.5 & work == 0

sum rods_per if work == 1, d
local lower1 = r(p1)
local upper1 = r(p99)
gen outlier = 0
replace outlier = 1 if (rods_per <= `lower1' | rods_per >= `upper1') & work == 1
drop if outlier == 1


xtset plantation_id_n

** labor supply - work_regs**
reg work hr_active, vce(cluster plantation_id_n)
estimates store work1
reg work hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store work2
reg work hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store work3

xml_tab work1 work2 work3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trimc_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) sheet(Work Day) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional - amount_regs **
reg daily_amount hr_active, vce(cluster plantation_id_n)
estimates store amount1
reg daily_amount hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store amount2
reg daily_amount hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store amount3

xml_tab amount1 amount2 amount3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trimc_nofe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Unconditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output unconditional **
reg daily_rods hr_active, vce(cluster plantation_id_n)
estimates store rods1
reg daily_rods hr_light hr_fair_very, vce(cluster plantation_id_n)
estimates store rods2
reg daily_rods hr_light hr_fair hr_very, vce(cluster plantation_id_n)
estimates store rods3

xml_tab rods1 rods2 rods3, save(${datadir}tables\Appendix\Worker Cluster\No FE\rods_regs_trimc_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional - amount_cond_regs **
reg daily_amount hr_active if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond1
reg daily_amount hr_light hr_fair_very if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond2
reg daily_amount hr_light hr_fair hr_very if work == 1, vce(cluster plantation_id_n)
estimates store amount_cond3

xml_tab amount_cond1 amount_cond2 amount_cond3, save(${datadir}tables\Appendix\Worker Cluster\No FE\T2_all_trimc_nofe.xls) append stats(N r2_a) format(SCLR3 NCLR3) sheet(Conditional Daily Earnings) keep(hr_active hr_light hr_fair_very hr_fair hr_very)

** daily output conditional **
reg daily_rods hr_active if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond1
reg daily_rods hr_light hr_fair_very if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond2
reg daily_rods hr_light hr_fair hr_very if work == 1, vce(cluster plantation_id_n)
estimates store rods_cond3

xml_tab rods_cond1 rods_cond2 rods_cond3, save(${datadir}tables\Appendix\Worker Cluster\No FE\rods_cond_regs_trimc_nofe.xls) replace stats(N r2_a) format(SCLR3 NCLR3) keep(hr_active hr_light hr_fair_very hr_fair hr_very)
}



*******************************
//   Main Paper Tables 3 4 & 5       //
*******************************

use "${datadir}Fitbit_Worker_AllDays_Trimmed_v3.dta", clear

*******************************************
// WORKER FIXED EFFECTS & WORKER CLUSTER //
*******************************************

// Trim A //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_uncond_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_uncond_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_work_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_work_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_uncond_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_uncond_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
local depvars "work_allday daily_amount_allday daily_rods_allday"

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_work_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_work_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local depvars "work_allday daily_amount_allday daily_rods_allday"
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_uncond_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_uncond_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_work_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_work_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_uncond_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_uncond_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_work_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_work_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_uncond_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_uncond_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_work_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_work_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_uncond_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_uncond_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_work_workerfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_work_workerfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Worker FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}

// Trim B //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_uncond_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_uncond_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_work_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_work_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_uncond_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_uncond_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_work_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_work_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_uncond_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_uncond_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_work_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_work_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Worker FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_uncond_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_uncond_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_work_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_work_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_uncond_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_uncond_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_work_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_work_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_uncond_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_uncond_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_work_workerfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_work_workerfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Worker FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}

// Trim C //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_uncond_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_uncond_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_work_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Labor_work_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_uncond_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_uncond_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_work_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Labor_work_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_uncond_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_uncond_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_work_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Labor_work_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Worker FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_uncond_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_uncond_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_work_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T3_Activity_work_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_uncond_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_uncond_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if rdtp == 1 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_work_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T4_Activity_work_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_uncond_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_uncond_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.plantation_id_n if  rdtp == 0 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_work_workerfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Worker FE\T5_Activity_work_workerfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Worker FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}

*************************************************
// GANG BY WEEK FIXED EFFECTS & WORKER CLUSTER //
*************************************************

// Trim A //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_uncond_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_uncond_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_work_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_work_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_uncond_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_uncond_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
local depvars "work_allday daily_amount_allday daily_rods_allday"

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_work_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_work_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local depvars "work_allday daily_amount_allday daily_rods_allday"
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_uncond_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_uncond_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_work_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_work_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_uncond_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_uncond_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_work_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_work_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_uncond_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_uncond_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_work_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_work_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_uncond_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_uncond_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_work_gangweek_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_work_gangweek_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Main Paper\") match(*.txt)

*dropping the previous estimates
eststo clear
}

// Trim B //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_uncond_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_uncond_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_work_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_work_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_uncond_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_uncond_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_work_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_work_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_uncond_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_uncond_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_work_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_work_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Main Paper\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_uncond_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_uncond_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_work_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_work_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_uncond_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_uncond_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_work_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_work_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_uncond_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_uncond_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_work_gangweek_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_work_gangweek_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Main Paper\") match(*.txt)

*dropping the previous estimates
eststo clear
}

// Trim C //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_uncond_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_uncond_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_work_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Labor_work_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_uncond_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_uncond_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_work_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Labor_work_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_uncond_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_uncond_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_work_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Labor_work_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Main Paper\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_uncond_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_uncond_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_work_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T3_Activity_work_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_uncond_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_uncond_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if rdtp == 1 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_work_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T4_Activity_work_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_uncond_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_uncond_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 gang##intweek if  rdtp == 0 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_work_gangweek_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Main Paper\T5_Activity_work_gangweek_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Main Paper\") match(*.txt)

*dropping the previous estimates
eststo clear
}


*****************************************
// GANG FIXED EFFECTS & WORKER CLUSTER //
*****************************************

// Trim A //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_uncond_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_uncond_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_work_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_work_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_uncond_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_uncond_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
local depvars "work_allday daily_amount_allday daily_rods_allday"

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_work_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_work_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local depvars "work_allday daily_amount_allday daily_rods_allday"
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_uncond_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_uncond_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_work_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_work_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_uncond_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_uncond_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_work_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_work_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_uncond_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_uncond_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_work_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_work_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_uncond_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_uncond_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_work_gang_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_work_gang_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}

// Trim B //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_uncond_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_uncond_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_work_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_work_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_uncond_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_uncond_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_work_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_work_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_uncond_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_uncond_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_work_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_work_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_uncond_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_uncond_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_work_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_work_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_uncond_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_uncond_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_work_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_work_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_uncond_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_uncond_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_work_gang_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_work_gang_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}

// Trim C //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_uncond_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_uncond_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_work_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Labor_work_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_uncond_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_uncond_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_work_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Labor_work_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_uncond_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_uncond_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_work_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Labor_work_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_uncond_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_uncond_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_work_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T3_Activity_work_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_uncond_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_uncond_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_work_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T4_Activity_work_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_uncond_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_uncond_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_work_gang_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Worker Cluster\Gang FE\T5_Activity_work_gang_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Worker Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}


***************************************
// GANG FIXED EFFECTS & GANG CLUSTER //
***************************************

// Trim A //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_uncond_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_uncond_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_work_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_work_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_uncond_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_uncond_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
local depvars "work_allday daily_amount_allday daily_rods_allday"

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_work_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_work_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local depvars "work_allday daily_amount_allday daily_rods_allday"
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_uncond_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_uncond_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_work_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_work_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_uncond_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_uncond_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_work_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_work_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_uncond_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_uncond_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_work_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_work_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_uncond_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_uncond_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_a == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_work_gangclus_gangfe_trima.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_work_gangclus_gangfe_trima.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}

// Trim B //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_uncond_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_uncond_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_work_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_work_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_uncond_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_uncond_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_work_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_work_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_uncond_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_uncond_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_work_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_work_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_uncond_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_uncond_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_work_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_work_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_uncond_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_uncond_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_work_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_work_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_uncond_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_uncond_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_b == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_work_gangclus_gangfe_trimb.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_work_gangclus_gangfe_trimb.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}

// Trim C //
{
local depvars "work_allday daily_amount_allday daily_rods_allday"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_uncond_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_uncond_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_work_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Labor_work_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_uncond_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_uncond_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_work_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Labor_work_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_uncond_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_uncond_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_work_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Labor_work_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear

////////////////////////////////////////

*then estimating the activity results with the trimmed data
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
*workreg1
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_uncond_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_uncond_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg2
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if work_allday == 1 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_work_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T3_Activity_work_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

////////////
*workreg3
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_uncond_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_uncond_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg4
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 1 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_work_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T4_Activity_work_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

//////////

*workreg5
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_uncond_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_uncond_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}

*workreg6
local depvars "hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act"
local j = 0
foreach outcome in `depvars' {
	local ++j
	reg `outcome' T_7 i.gang if rdtp == 0 & work_allday == 1 & trimmed_obs_c == 0, vce(cluster gang)
	eststo `outcome'_EG
	if `j' == 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_work_gangclus_gangfe_trimc.xls", replace eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
	if `j' > 1 {
	outreg2 `outcome' using "${datadir}tables\Appendix\Gang Cluster\Gang FE\T5_Activity_work_gangclus_gangfe_trimc.xls", eqkeep(N mean sd) keep(T_7 Constant) dec(3)
	}
}
rmfiles , folder("${datadir}tables\Appendix\Gang Cluster\Gang FE\") match(*.txt)

*dropping the previous estimates
eststo clear
}


***************************************
** Manski Bounds on Tables 7 8 and 9 **
***************************************
{


clear
clear matrix
set more off
set seed 250531

use "${datadir}Fitbit_Worker_AllDays_Trimmed_v3.dta", clear
eststo clear

gen work1 = work_allday
gen daily_amount1 = daily_amount_allday
gen daily_rods1 = daily_rods_allday

tempfile prepared_data
save `prepared_data', replace

// Trim A //
{
// Work reg 1 //
{
use `prepared_data', clear
eststo clear
ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		*Getting the baseline average number of `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		*Calculating the exact difference

		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis "control attrition ratio"
		dis `control_attr_ratio'
		
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis "treatment attrition ratio"
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis "difference in attrition ratios (c - t)"
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis "num_obs_treat"
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower treat trim"
			dis `lower'
			local upper = r(p99)
			dis "upper treat trim"
			dis `upper'

			dis "`var' before lower replacement"
			sum `var'
			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			dis "`var' after lower replacement"
			
			sum `var'
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			dis "`var' before upper replacement"
			sum `var'
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			dis "`var' after upper replacement"
			sum `var'			
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis "num_obs_control"
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower control trim"
			dis `lower'
			local upper = r(p99)
			dis "upper control trim"
			dis `upper'

			dis "`var' before replacement"
			sum `var'
			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			dis "`var' after replacement"
			sum `var'
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
*
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trima_manski.xls) stats(N num_obs_control num_obs_treat) keep(T_7 _cons) sd2 replace sheet(T3 - ITT all) format(SCLR3 NCLR3)
}


// Work reg 2 //
{
use `prepared_data', clear
eststo clear
// drop work1_* daily_amount1_* daily_rods1_*

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis "control attrition ratio"
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis "treatment attrition ratio"
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis "difference in attrition ratios (c - t)"
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis "num_obs_treat"
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower treat trim"
			dis `lower'
			local upper = r(p99)
			dis "upper treat trim"
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}

		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis "num_obs_control"
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis "lower control trim"
			dis `lower'
			local upper = r(p99)
			dis "upper control trim"
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T3 - ITT work only) format(SCLR3 NCLR3)
}

// Work reg 3 //
{
use `prepared_data', clear
eststo clear
// drop work1_* daily_amount1_* daily_rods1_*

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT all) format(SCLR3 NCLR3)
}

// Work reg 4 //
{
use `prepared_data', clear

eststo clear
// drop work1_* daily_amount1_* daily_rods1_*

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT work only) format(SCLR3 NCLR3)
}

// Work reg 5 //
{
use `prepared_data', clear

eststo clear
// drop work1_* daily_amount1_* daily_rods1_*

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT all) format(SCLR3 NCLR3)
}

// Work reg 6 //
{
use `prepared_data', clear

eststo clear
// drop work1_* daily_amount1_* daily_rods1_*

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT work only) format(SCLR3 NCLR3)
}

// Activity reg 1 //
{
use `prepared_data', clear

eststo clear
// drop work1_* daily_amount1_* daily_rods1_*

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 replace sheet(T3 - ITT all) format(SCLR3 NCLR3)
}

// Activity reg 2 //
{
use `prepared_data', clear

eststo clear
// drop *_randomnum *_control_trim *_treat_trim

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T3 - ITT work only) format(SCLR3 NCLR3)
}

// Activity reg 3 //
{
use `prepared_data', clear

eststo clear
// drop *_randomnum *_control_trim *_treat_trim

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT all) format(SCLR3 NCLR3)
}

// Activity reg 4 //
{
use `prepared_data', clear

eststo clear
// drop *_randomnum *_control_trim *_treat_trim

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT work only) format(SCLR3 NCLR3)
}

// Activity reg 5 //
{
use `prepared_data', clear

eststo clear
// drop *_randomnum *_control_trim *_treat_trim

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT all) format(SCLR3 NCLR3)
}

// Activity reg 6 //
{
use `prepared_data', clear

eststo clear
// drop *_randomnum *_control_trim *_treat_trim

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_a == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_a == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_a == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_a == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_a = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_a == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trima_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT work only) format(SCLR3 NCLR3)
}
}

// Trim B //
{
// Work reg 1 //
{
use `prepared_data', clear

eststo clear
ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		*Getting the baseline average number of `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		*Calculating the exact difference

		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis "control attrition ratio"
		dis `control_attr_ratio'
		
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis "treatment attrition ratio"
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis "difference in attrition ratios (c - t)"
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis "num_obs_treat"
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower treat trim"
			dis `lower'
			local upper = r(p99)
			dis "upper treat trim"
			dis `upper'

			dis "`var' before lower replacement"
			sum `var'
			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			dis "`var' after lower replacement"
			
			sum `var'
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			dis "`var' before upper replacement"
			sum `var'
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			dis "`var' after upper replacement"
			sum `var'			
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis "num_obs_control"
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower control trim"
			dis `lower'
			local upper = r(p99)
			dis "upper control trim"
			dis `upper'

			dis "`var' before replacement"
			sum `var'
			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			dis "`var' after replacement"
			sum `var'
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
*
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimb_manski.xls) stats(N num_obs_control num_obs_treat) keep(T_7 _cons) sd2 replace sheet(T3 - ITT all) format(SCLR3 NCLR3)
}


// Work reg 2 //
{
use `prepared_data', clear

eststo clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis "control attrition ratio"
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis "treatment attrition ratio"
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis "difference in attrition ratios (c - t)"
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis "num_obs_treat"
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower treat trim"
			dis `lower'
			local upper = r(p99)
			dis "upper treat trim"
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}

		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis "num_obs_control"
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis "lower control trim"
			dis `lower'
			local upper = r(p99)
			dis "upper control trim"
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T3 - ITT work only) format(SCLR3 NCLR3)
}

// Work reg 3 //
{
use `prepared_data', clear

eststo clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT all) format(SCLR3 NCLR3)
}

// Work reg 4 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT work only) format(SCLR3 NCLR3)
}

// Work reg 5 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT all) format(SCLR3 NCLR3)
}

// Work reg 6 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT work only) format(SCLR3 NCLR3)
}

// Activity reg 1 //
{
eststo clear
use `prepared_data', clear
// set seed 320603

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 replace sheet(T3 - ITT all) format(SCLR3 NCLR3)
}

// Activity reg 2 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T3 - ITT work only) format(SCLR3 NCLR3)
}

// Activity reg 3 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT all) format(SCLR3 NCLR3)
}

// Activity reg 4 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT work only) format(SCLR3 NCLR3)
}

// Activity reg 5 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT all) format(SCLR3 NCLR3)
}

// Activity reg 6 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_b == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_b == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_b == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_b == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_b = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_b == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimb_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT work only) format(SCLR3 NCLR3)
}
}

// Trim C //
{
// Work reg 1 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		*Getting the baseline average number of `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		*Calculating the exact difference

		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis "control attrition ratio"
		dis `control_attr_ratio'
		
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis "treatment attrition ratio"
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis "difference in attrition ratios (c - t)"
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis "num_obs_treat"
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower treat trim"
			dis `lower'
			local upper = r(p99)
			dis "upper treat trim"
			dis `upper'

			dis "`var' before lower replacement"
			sum `var'
			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			dis "`var' after lower replacement"
			
			sum `var'
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			dis "`var' before upper replacement"
			sum `var'
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			dis "`var' after upper replacement"
			sum `var'			
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis "num_obs_control"
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower control trim"
			dis `lower'
			local upper = r(p99)
			dis "upper control trim"
			dis `upper'

			dis "`var' before replacement"
			sum `var'
			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			dis "`var' after replacement"
			sum `var'
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
*
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimc_manski.xls) stats(N num_obs_control num_obs_treat) keep(T_7 _cons) sd2 replace sheet(T3 - ITT all) format(SCLR3 NCLR3)
}


// Work reg 2 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis "control attrition ratio"
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis "treatment attrition ratio"
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis "difference in attrition ratios (c - t)"
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis "num_obs_treat"
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis "lower treat trim"
			dis `lower'
			local upper = r(p99)
			dis "upper treat trim"
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}

		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis "num_obs_control"
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis "lower control trim"
			dis `lower'
			local upper = r(p99)
			dis "upper control trim"
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T3 - ITT work only) format(SCLR3 NCLR3)
}

// Work reg 3 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT all) format(SCLR3 NCLR3)
}

// Work reg 4 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT work only) format(SCLR3 NCLR3)
}

// Work reg 5 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT all) format(SCLR3 NCLR3)
}

// Work reg 6 //
{
eststo clear
use `prepared_data', clear

ds work1 daily_amount1 daily_rods1
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_control_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab work1_lower work1_upper daily_amount1_lower daily_amount1_upper daily_rods1_lower daily_rods1_upper, save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Labor_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT work only) format(SCLR3 NCLR3)
}

// Activity reg 1 //
{
eststo clear
use `prepared_data', clear
// set seed 320603

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 replace sheet(T3 - ITT all) format(SCLR3 NCLR3)
}

// Activity reg 2 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T3 - ITT work only) format(SCLR3 NCLR3)
}

// Activity reg 3 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT all) format(SCLR3 NCLR3)
}

// Activity reg 4 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 1 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T4 - TOT work only) format(SCLR3 NCLR3)
}

// Activity reg 5 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT all) format(SCLR3 NCLR3)
}

// Activity reg 6 //
{
eststo clear
use `prepared_data', clear

local activity_estimates "hr_sedentary_lower hr_sedentary_upper hr_light_lower hr_light_upper hr_fair_lower hr_fair_upper hr_very_lower hr_very_upper r_lig_act_lower r_lig_act_upper r_fai_act_lower r_fai_act_upper r_ver_act_lower r_ver_act_upper"

ds hr_sedentary hr_light hr_fair hr_very r_lig_act r_fai_act r_ver_act
foreach var in `r(varlist)' {
		*Making variables that simulate those that the regressions work on (trimmed)
		gen `var'_control_trim = `var'
		replace `var'_control_trim = . if T_7 == 1 | trimmed_obs_c == 1
		qui sum `var'_control_trim

		gen `var'_treat_trim = `var'
		replace `var'_treat_trim = . if T_7 == 0 | trimmed_obs_c == 1
		qui sum `var'_treat_trim


		*Getting the baseline average `var' values on control vs treatment 
		qui sum `var' if T_7 == 0
		local `var'_control_n = r(N)

		qui sum `var' if T_7 == 1
		local `var'_treat_n = r(N)


		*Seeing which group, control or treatment, was affected more by trimming
		qui sum `var' if trimmed_obs_c == 0 & T_7 == 0
		local `var'_control_n_trimmed = r(N)

		qui sum `var' if trimmed_obs_c == 0 & T_7 == 1
		local `var'_treat_n_trimmed = r(N)

		*Getting the exact difference
		local control_attr_ratio = (``var'_control_n_trimmed' / ``var'_control_n')
		dis `control_attr_ratio'
		local treat_attr_ratio = (``var'_treat_n_trimmed' / ``var'_treat_n')
		dis `treat_attr_ratio'

		local diff_attr_ratio = ((`control_attr_ratio' - `treat_attr_ratio'))
		dis `diff_attr_ratio'

	// add if and else options for control and treatment here
		if `diff_attr_ratio' > 0 {
		
			*Setting the number of obs for which we will do the replacement
			local num_obs_treat = round(( ``var'_treat_n' * `diff_attr_ratio' ))
			dis `num_obs_treat'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_treat `num_obs_treat')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_treat' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_treat' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_treat `num_obs_treat')
			}
		}
		else {
				*Setting the number of obs for which we will do the replacement
			local num_obs_control = abs(round(( ``var'_control_n' * `diff_attr_ratio' )))
			dis `num_obs_control'

			egen `var'_randomnum =  rank(runiform())

			sum `var'_treat_trim, d
			local lower = r(p1)
			dis `lower'
			local upper = r(p99)
			dis `upper'

			replace `var' = `lower' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_lower, addscalars(num_obs_control `num_obs_control')
			}
			
			replace `var' = `upper' if `var'_randomnum <= ( `num_obs_control' )
			replace trimmed_obs_c = 0 if `var'_randomnum <= ( `num_obs_control' )


			foreach outcome in `var' {
					reg `outcome' T_7 gang##intweek if work_allday == 1 & rdtp == 0 & trimmed_obs_c == 0, vce(cluster plantation_id_n)
					eststo `outcome'_upper, addscalars(num_obs_control `num_obs_control')
			} 
	}
}
xml_tab `activity_estimates', save(${datadir}tables\Appendix\Worker Cluster\GangXweek FE\T3_T4_T5_Activity_trimc_manski.xls) stats(N  num_obs_control num_obs_treat) keep(T_7 _cons) sd2 append sheet(T5 - TMUT work only) format(SCLR3 NCLR3)
}

}


}


rmfiles , folder("${datadir}tables\Appendix\") match(*.txt)






